package com.leostadyn.stackfarm.timestatistics.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.model.bean.PostBean;

import java.util.ArrayList;
import java.util.List;

public class SelectListAdapter extends RecyclerView.Adapter<SelectListAdapter.ViewHolder> {
    public Context mContext;
    private final int COUNT=6;
    private List<PostBean> listDataBeans;
    private ArrayList<String> postidLists = new ArrayList<>();
    int index = -1;
    private String mIndex = "-1";
    private ArrayList<String> projectIdList = new ArrayList<>();
    private ArrayList<String> nameLists = new ArrayList<>();
    private ArrayList<String> codeLists = new ArrayList<>();

    public SelectListAdapter(Context mContext, List<PostBean> listDataBeans, ArrayList<String> listids) {
        this.mContext = mContext;
        this.listDataBeans = listDataBeans;
        if (listids != null) {
            this.postidLists.addAll(postidLists);
        }
    }

    public void setmIndex(String Index) {

        if (!Index.equals("-1")) {
            mIndex = Index;
            index = Integer.parseInt(mIndex) - 1;
        }

    }

    public void setListDataBeans(List<PostBean> listDataBeans) {
        this.listDataBeans = listDataBeans;
    }

    public List<PostBean> getListDataBeans() {
        return listDataBeans;
    }

    public ArrayList<String> getProjectIdList() {
        return projectIdList;
    }

    public void setProjectIdList(ArrayList<String> projectIdList) {
        this.projectIdList = projectIdList;
    }

    public ArrayList<String> getPostidLists() {
        return postidLists;
    }

    public void setPostidLists(ArrayList<String> postidLists) {
        this.postidLists = postidLists;
    }

    public ArrayList<String> getNameLists() {
        return nameLists;
    }

    public void setNameLists(ArrayList<String> nameLists) {
        this.nameLists = nameLists;
    }

    public ArrayList<String> getCodeLists() {
        return codeLists;
    }

    public void setCodeLists(ArrayList<String> codeLists) {
        this.codeLists = codeLists;
    }

    @Override
    public void onBindViewHolder(@NonNull SelectListAdapter.ViewHolder viewHolder, int i) {
        PostBean bean = listDataBeans.get(i);
        viewHolder.iv_check.setImageResource(R.drawable.check);
        if (index > -1) {
            listDataBeans.get(index).setSelected(true);
            postidLists.add(index + 1 + "");
            nameLists.add(listDataBeans.get(index).getName());
            projectIdList.add(listDataBeans.get(index).getId());
            codeLists.add(listDataBeans.get(index).getCode());
        }

        //下标相同则显示图片
        if (postidLists.contains(bean.getId()))
        {
            viewHolder.iv_check.setVisibility(View.VISIBLE);
            if (!nameLists.contains(bean.getName())) {
                nameLists.add(bean.getName());
            }
        }
        else{
            viewHolder.iv_check.setVisibility(View.INVISIBLE);
        }
        viewHolder.iv_check.setVisibility(listDataBeans.get(i).isSelected() ? View.VISIBLE : View.INVISIBLE);
        if (listDataBeans.get(i).getCode() != null)
            viewHolder.tv_jop.setText(listDataBeans.get(i).getCode() + "\t\t\t\t" + listDataBeans.get(i).getName());
            else
                viewHolder.tv_jop.setText(listDataBeans.get(i).getName());


        viewHolder.itemView.setOnClickListener(v->{

            postidLists.clear();
            nameLists.clear();
            projectIdList.clear();
            codeLists.clear();
            postidLists.add(i + 1 + "");
            nameLists.add(listDataBeans.get(i).getName());
            projectIdList.add(listDataBeans.get(i).getId());
            if (codeLists != null) {
                codeLists.add(listDataBeans.get(i).getCode());
            }

            for (PostBean post : listDataBeans) {
                post.setSelected(false);
            }
            listDataBeans.get(i).setSelected(true);
            notifyDataSetChanged();
            index = -1;
        });

    }

    @NonNull
    @Override
    public SelectListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_job_list, viewGroup, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_jop;
        private ImageView iv_check;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_jop = itemView.findViewById(R.id.tv_group);
            iv_check = itemView.findViewById(R.id.iv_check);
        }
    }

    @Override
    public int getItemCount() {
        if (listDataBeans == null) {
            return 0;
        }
        return listDataBeans.size();
    }


}
